package com.net.TCP04.char2;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

/**
 * 接收线程
 *
 * @author jiangbang
 * @date 2016/8/11
 */
public class Receive implements Runnable{
    private DataInputStream dis;
    //控制运行
    private boolean isRunning = true;
    public Receive(){
    }
    public Receive(Socket client){
        try {
            dis = new DataInputStream(client.getInputStream());
        } catch (IOException e) {
            isRunning = false; //出异常 停止线程
            CloseUtil.ColseAll(dis);
        }
    }
    //接收数据
    public String  receive(){
        String msg = "";
        try {
            msg = dis.readUTF();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return msg;
    }

    @Override
    public void run() {
        while(isRunning){
            System.out.println(receive());
        }
    }
}
